from django.db import models
from BYD_ERP.utils.base_model import BaseModel2

# python ../../manage.py startapp basic_info

# 迁移基础：erp_system

# python ../../manage.py makemigrations basic_info
# python ../../manage.py migrate basic_info

# python ../../manage.py check

# 国家模型类：
class NationModel(models.Model):
    id = models.IntegerField(primary_key=True)
    n_name = models.CharField('国家名称', max_length=30)

    class Meta:
        db_table = 'nation'
        verbose_name = '国家表'
        verbose_name_plural = verbose_name
        app_label = 'basic_info'
        ordering = ['id']

        def __str__(self):
            return self.n_name


# 省份模型类：
class ProvinceModel(models.Model):
    id = models.IntegerField(primary_key=True)
    p_name = models.CharField('省份名称', max_length=30)
    nation = models.ForeignKey('NationModel', related_name='province_list', on_delete=models.CASCADE)

    class Meta:
        db_table = 'province'
        verbose_name = '省份表'
        verbose_name_plural = verbose_name
        ordering = ['id']

        def __str__(self):
            return self.p_name

# 地级市模型类：
class CityModel(models.Model):
    id = models.IntegerField(primary_key=True)
    c_name = models.CharField('地级市名称', max_length=30)
    province = models.ForeignKey('ProvinceModel', related_name='city_list', on_delete=models.CASCADE)

    class Meta:
        db_table = 'city'
        verbose_name = '地级市表'
        verbose_name_plural = verbose_name
        ordering = ['id']

        def __str__(self):
            return self.c_name

# 县级市模型类：
class CountyModel(models.Model):
    id = models.IntegerField(primary_key=True)
    cc_name = models.CharField('县级市名称', max_length=30)
    city = models.ForeignKey('CityModel', related_name='county_list', on_delete=models.CASCADE)

    class Meta:
        db_table = 'county'
        verbose_name = '县级市表'
        verbose_name_plural = verbose_name
        ordering = ['id']

        def __str__(self):
            return self.cc_name


# 供应商模型类：
class SupplierModel(BaseModel2):
    name = models.CharField(max_length=100, verbose_name='供应商全称', unique=True)
    ratepayer_number = models.CharField('统一社会信用代码', max_length=50, blank=True, null=True)
    contacts_name = models.CharField('联系人姓名', max_length=22, blank=True, null=True)
    mobile = models.CharField('手机号码', max_length=11, blank=True, null=True)
    phone = models.CharField('联系电话', max_length=22, blank=True, null=True)
    email = models.CharField('电子邮箱', max_length=50, blank=True, null=True)
    category_name = models.CharField('供应商类别', max_length=50, blank=True, null=True)   # 原材料还是辅助材料
    bank = models.CharField('开户行', max_length=50, blank=True, null=True)
    account_number = models.CharField('银行账号', max_length=50, blank=True, null=True)
    nation = models.CharField('国家', max_length=50, blank=True, null=True)
    province = models.CharField('省份', max_length=50, blank=True, null=True)
    city = models.CharField('地级市', max_length=50, blank=True, null=True)
    county = models.CharField('县级市', max_length=50, blank=True, null=True)
    address = models.CharField('详细地址', max_length=50, blank=True, null=True)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)
    init_pay = models.DecimalField('期初应付', max_digits=20, decimal_places=2, blank=True, null=True)      # 精确到分
    current_pay = models.DecimalField('期末应付(当前应付)', max_digits=20, decimal_places=2, blank=True, null=True)   # 精确到分
    order_number = models.IntegerField('排序号码', default=100)

    class Meta:
        db_table = 't_supplier'
        verbose_name = '供应商'
        verbose_name_plural = verbose_name
        ordering = ['order_number', 'id']

    def __str__(self):
        return self.name

# 客户类型
customer_category_choice = (
    ('1', '配套'),
    ('2', '市场')
)

# 客户模型类：
class CustomerModel(BaseModel2):
    name = models.CharField(max_length=100, verbose_name='客户全称', unique=True)
    abbreviation = models.CharField('客户简称', max_length=50, blank=True, null=True)
    ratepayer_number = models.CharField('统一社会信用代码', max_length=50, blank=True, null=True)
    salesman = models.CharField('业务员姓名', max_length=22, blank=True, null=True)
    mobile = models.CharField('业务员手机号码', max_length=11, blank=True, null=True)
    phone = models.CharField('业务员联系电话', max_length=22, blank=True, null=True)
    email = models.CharField('业务员电子邮箱', max_length=50, blank=True, null=True)
    district_manager = models.CharField('片区负责人姓名', max_length=50, blank=True, null=True)
    d_mobile = models.CharField('片区负责人手机号', max_length=50, blank=True, null=True)
    category_name = models.CharField('客户类别', choices=customer_category_choice, max_length=5, blank=True, null=True)   # 配套还是市场
    bank = models.CharField('开户行', max_length=50, blank=True, null=True)
    account_number = models.CharField('银行账号', max_length=50, blank=True, null=True)
    nation = models.CharField('国家', max_length=50, blank=True, null=True)
    province = models.CharField('省份', max_length=50, blank=True, null=True)
    city = models.CharField('地级市', max_length=50, blank=True, null=True)
    county = models.CharField('县级市', max_length=50, blank=True, null=True)
    address = models.CharField('详细地址', max_length=50, blank=True, null=True)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)
    init_receivable = models.DecimalField('期初应收', max_digits=20, decimal_places=2, blank=True, null=True)      # 精确到分
    current_receivable = models.DecimalField('期末应收', max_digits=20, decimal_places=2, blank=True, null=True)   # 精确到分
    order_number = models.IntegerField('排序号码', default=100)

    class Meta:
        db_table = 't_customer'
        verbose_name = '客户'
        verbose_name_plural = verbose_name
        ordering = ['order_number', 'id']

    def __str__(self):
        return self.name

# 仓库模型类：
class WarehouseModel(BaseModel2):
    name = models.CharField(max_length=100, verbose_name='仓库名称', unique=True)
    category_name = models.CharField('仓库类别', max_length=50, blank=True, null=True)
    # 1成品库、2包装库、3改商标包装库、4原材料库、5辅助材料库、6油品库、7车间材料库、8黑压胶库
    nation = models.CharField('国家', max_length=50, blank=True, null=True)
    province = models.CharField('省份', max_length=50, blank=True, null=True)
    city = models.CharField('地级市', max_length=50, blank=True, null=True)
    county = models.CharField('县级市', max_length=50, blank=True, null=True)
    address = models.CharField('详细地址', max_length=50, blank=True, null=True)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)
    warehouse_fee = models.DecimalField('仓储费用（元/天/KG）', max_digits=10, decimal_places=2, blank=True, null=True)
    truckage = models.DecimalField('搬运费用', max_digits=10, decimal_places=2, blank=True, null=True)
    order_number = models.IntegerField('排序号码', default=100)
    is_default = models.BooleanField('是否默认的仓库', default=False)
    # 用户模型来自于erp_system的app中，必须要加前缀。
    leader_user = models.ForeignKey('erp_system.UserModel', verbose_name='负责人', null=True, blank=True, on_delete=models.CASCADE)

    class Meta:
        db_table = 't_warehouse'
        verbose_name = '仓库'
        verbose_name_plural = verbose_name
        ordering = ['order_number', 'id']

    def __str__(self):
        return self.name
    pass

# 结算账户模型类
class SettlementAccountModel(BaseModel2):
    name = models.CharField(max_length=100, verbose_name='账户名称', unique=True)
    number_code = models.CharField('编号', max_length=28, unique=True)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)
    init_amount = models.DecimalField('期初余额', max_digits=10, decimal_places=2, blank=True, null=True)
    balance = models.DecimalField('当前余额', max_digits=10, decimal_places=2, blank=True, null=True)
    order_number = models.IntegerField('排序号码', default=100)
    is_default = models.BooleanField('是否默认', default=False)

    class Meta:
        db_table = 't_settlement_account'
        verbose_name = '结算账户'
        verbose_name_plural = verbose_name
        ordering = ['order_number', 'id']

    def __str__(self):
        return self.name


# 模具状态
mould_status_choice = (
    ('1', '正常'),
    ('2', '导管响'),
    ('3', '壁薄'),
    ('4', '上堵头维修'),
    ('5', '上圆盘维修'),
    ('6', '有深度磕碰')
)

# original：原始的，起初的
# ordinary：普通的，平常的
# common：普通的，常见的
# 模具模型类
###############################################
# 1.独立一个app，设备管理
# 2.模型名字MouldModel改为MouldCommonModel
###############################################
#    限指：多楔带、齿形带的普通模具
class MouldModel(BaseModel2):
    name = models.CharField(max_length=100, verbose_name='模具型号', unique=True)
    number_code = models.CharField('编号', max_length=28, blank=True, null=True)
    diameter = models.CharField('模具直径', max_length=28, blank=True, null=True)
    perimeter = models.CharField('模具周长', max_length=28, blank=True, null=True)
    date_in_produced = models.CharField('生产日期', max_length=28, blank=True, null=True)
    useful_life = models.CharField('使用寿命', max_length=28, blank=True, null=True)
    status = models.CharField('模具的状态：正常或需要维修', choices=mould_status_choice, max_length=10, default=1, blank=True, null=True)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)

    class Meta:
        db_table = 't_mould_common'
        verbose_name = '常规的模具表'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return self.name
